#ifndef PWM_H
#define PWM_H

//////////////////////////////////////////////////////////////////////////////////////////////
//for FOSC = 24MHz, predefined values:

#define PWM_8000KHZ		2
#define PWM_4000KHZ		5
#define PWM_2000KHZ		11
#define PWM_1000HZ		23
#define PWM_800KHZ		29
#define PWM_400KHZ		59
#define PWM_200KHZ		119
#define PWM_100KHZ		239
#define PWM_60KHZ			399
#define PWM_50KHZ			479
#define PWM_10KHZ			2400
#define PWM_5KHZ			4800
#define PWM_2940HZ		8166
#define PWM_1667HZ		14410
#define PWM_1KHZ			24020
#define PWM_732HZ			32767

#define PWM_MAX			PWM_8000KHZ
#define PWM_MIN			PWM_732HZ

//////////////////////////////////////////////////////////////////////////////////////////////

#define PWM_INIT(x)					if((x)==0)PWMCR &= ~0x40;else PWMCR |= 0x40
#define PWM_SET_COUNT(x)		PWMC = (x)
#define PWM_START()					PWMCR |= 0x80
#define PWM_STOP()					PWMCR	&= ~0x80


#define PWM0_INIT(x)						if((x)==0)PWM0CR &= ~0x40;else PWM0CR |= 0x40
#define PWM0_SET_T1(x)					PWM0T1 = (x)
#define PWM0_SET_T2(x)					PWM0T2 = (x)
#define PWM0_START()						PWM0CR |= 0x80
#define PWM0_STOP()							PWM0CR &= ~0x80

#define PWM1_INIT(x)						if((x)==0)PWM1CR &= ~0x40;else PWM1CR |= 0x40
#define PWM1_SET_T1(x)					PWM1T1 = (x)
#define PWM1_SET_T2(x)					PWM1T2 = (x)
#define PWM1_START()						PWM1CR |= 0x80
#define PWM1_STOP()							PWM1CR &= ~0x80

#define PWM2_INIT(x)						if((x)==0)PWM2CR &= ~0x40;else PWM2CR |= 0x40
#define PWM2_SET_T1(x)					PWM2T1 = (x)
#define PWM2_SET_T2(x)					PWM2T2 = (x)
#define PWM2_START()						PWM2CR |= 0x80
#define PWM2_STOP()							PWM2CR &= ~0x80

#define PWM3_INIT(x)						if((x)==0)PWM3CR &= ~0x40;else PWM3CR |= 0x40
#define PWM3_SET_T1(x)					PWM3T1 = (x)
#define PWM3_SET_T2(x)					PWM3T2 = (x)
#define PWM3_START()						PWM3CR |= 0x80
#define PWM3_STOP()							PWM3CR &= ~0x80

#define PWM4_INIT(x)						if((x)==0)PWM4CR &= ~0x40;else PWM4CR |= 0x40
#define PWM4_SET_T1(x)					PWM4T1 = (x)
#define PWM4_SET_T2(x)					PWM4T2 = (x)
#define PWM4_START()						PWM4CR |= 0x80
#define PWM4_STOP()							PWM4CR &= ~0x80

#define PWM5_INIT(x)						if((x)==0)PWM5CR &= ~0x40;else PWM5CR |= 0x40
#define PWM5_SET_T1(x)					PWM5T1 = (x)
#define PWM5_SET_T2(x)					PWM5T2 = (x)
#define PWM5_START()						PWM5CR |= 0x80
#define PWM5_STOP()							PWM5CR &= ~0x80

#define PWM6_INIT(x)						if((x)==0)PWM6CR &= ~0x40;else PWM6CR |= 0x40
#define PWM6_SET_T1(x)					PWM6T1 = (x)
#define PWM6_SET_T2(x)					PWM6T2 = (x)
#define PWM6_START()						PWM6CR |= 0x80
#define PWM6_STOP()							PWM6CR &= ~0x80

#define PWM7_INIT(x)						if((x)==0)PWM7CR &= ~0x40;else PWM7CR |= 0x40
#define PWM7_SET_T1(x)					PWM7T1 = (x)
#define PWM7_SET_T2(x)					PWM7T2 = (x)
#define PWM7_START()						PWM7CR |= 0x80
#define PWM7_STOP()							PWM7CR &= ~0x80




#define PWMx_INIT(i, x)					if((x) == 0)																																	\
																	(*(unsigned char volatile xdata *)(0xff04+0x10*i)) &= ~0x40;								\
																else																																					\
																	(*(unsigned char volatile xdata *)(0xff04+0x10*i)) |= 0x40;

#define PWMx_SET_CONFIG(i,  x)	(*(unsigned char volatile xdata *)(0xff04+0x10*i)) = (x)																
#define PWMx_SET_T1(i,  x)			(*(unsigned int  volatile xdata *)(0xff00+0x10*i)) = (x)
#define PWMx_SET_T2(i,	x)			(*(unsigned int  volatile xdata *)(0xff02+0x10*i)) = (x)
#define PWMx_START(i)						(*(unsigned char volatile xdata *)(0xff04+0x10*i)) |= 0x80
#define PWMx_STOP(i)						(*(unsigned char volatile xdata *)(0xff04+0x10*i)) &= ~0x80
																	


#endif